import { Ast, Visitor, createRootAst } from "./ast";
import { CoreVisitor } from './core';
import { PACKAGES } from "./utils";
/**
 * 所在页面判断
 */
export class PageContext {
    is?: boolean;
}
export abstract class PageVisitor implements Visitor {
    visitNode(node: Ast, ctx: PageContext) {
        if (!node.visibleToUser) return;
        if (node.className) {
            const classNameSplit = node.className.split('.')
            const classNameEnd = classNameSplit.pop()
            const methodName = `visit${classNameEnd.charAt(0).toUpperCase() + classNameEnd.slice(1)}`
            if (this[methodName]) return this[methodName](node, ctx)
        }
        node.children.map(c => c.visit(this, ctx))
    }
}

export class MinePageVisitor extends PageVisitor {
    visitRelativeLayout(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`profile_settings_button_container`)) {
            ctx.is = true;
            return true;
        }
        return node.children.map(it => it.visit(this, ctx))
    }
}

export function isMinePage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new MinePageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

export class MenuPageVisitor extends PageVisitor {
    visitDrawerLayout(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`side_menu`) && node.visibleToUser) {
            ctx.is = true;
            return;
        }
        return node.children.map(it => it.visit(this, ctx))
    }
}

export function isMenuPage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new MenuPageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

export class MineMoneyPageVisitor extends PageVisitor {
    visitTextView(node: Ast, ctx: PageContext) {
        if (node.text === `快币(充值)` && node.visibleToUser) {
            ctx.is = true;
            return;
        }
        return node.children.map(it => it.visit(this, ctx))
    }
}


export function isMineMoneyPage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new MineMoneyPageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}


/**
 * 直播广场
 */
export class LiveSquarePageVisitor extends PageVisitor {
    visitRecyclerView(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`live_explore_fragment_recycler_view`) && node.visibleToUser) {
            ctx.is = true;
            return;
        }
        return node.children.map(it => it.visit(this, ctx))
    }
}


/**
 * 个人资料页
 * @returns 
 */

export class MineProfilePageVisitor extends PageVisitor {
    visitTextView(node: Ast, ctx: PageContext) {
        if (node.text === `更换` && node.parent.clickable) {
            ctx.is = true;
        }
    }
}

export class OtherProfilePageVisitor extends PageVisitor {
    visitViewPager(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`profile_view_pager`)) {
            ctx.is = true;
        }
    }
}


export function isOtherProfilePage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new OtherProfilePageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

/**
 * 设置昵称
 * @returns 
 */

export class MineProfileNicknameSettingPageVisitor extends PageVisitor {
    visitTextView(node: Ast, ctx: PageContext) {
        if (node.text === `设置昵称`) {
            ctx.is = true;
        }
    }
}


export class MineProfileAvatarSettingPageVisitor extends PageVisitor {
    visitLinearLayout(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`avatar_choose_picture_container`)) {
            ctx.is = true;
        }
    }
}

export class MineProfileAvatarUploadPageVisitor extends PageVisitor {
    visitRecyclerView(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`album_view_list`)) {
            ctx.is = true;
        }
    }
}

export class ToProfileFirstVideoVisitor extends PageVisitor {
    visitRelativeLayout(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`player_cover_container`)) {
            if (node.children.length === 0) {
                ctx.is = true;
                return;
            }
        }
        node.children.map(c => c.visit(this, ctx))
    }
}

export function isLiving() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new ToProfileFirstVideoVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

/**
 * 背景图设置页面
 */
export class MineProfileBgSettingPageVisitor extends PageVisitor {
    visitImageView(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`photo_preview_bg`)) {
            ctx.is = true;
        }
    }
}
export class MineProfileBgUploadPageVisitor extends PageVisitor {
    visitRecyclerView(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`album_view_list`)) {
            ctx.is = true;
        }
    }
}

export function isMineProfileBgUploadPage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new MineProfileBgUploadPageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

export function isMineProfileBgSettingPage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new MineProfileBgSettingPageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

export function isMineProfileAvatarUploadPage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new MineProfileAvatarUploadPageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

export function IsMineProfileAvatarSettingPage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new MineProfileAvatarSettingPageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

export function isMineProfileNicknameSettingPage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new MineProfileNicknameSettingPageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

export function isMineProfilePage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new MineProfilePageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

export function isLiveSquarePage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new LiveSquarePageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

export class HomePageVisitor extends PageVisitor {
    visitHorizontalScrollView(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`kcube_tab_strip`)) {
            ctx.is = true;
            return;
        }
        return node.children.map(it => it.visit(this, ctx))
    }
}

export function isHomePage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new HomePageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

/**
 * 首页-关注
 */
export class HomeFollowPageVisitor extends PageVisitor {
    visitFrameLayout(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`follow_header_pymi`)) {
            ctx.is = true;
            return;
        }
        return node.children.map(it => it.visit(this, ctx))
    }
}

export function isHomeFollowPage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new HomeFollowPageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

/**
 * 直播页
 */
export class LiveRoomPageVisitor extends PageVisitor {
    visitFrameLayout(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`live_play_root_container`)) {
            ctx.is = true;
            return;
        }
        return node.children.map(it => it.visit(this, ctx))
    }
}


export class IsJoinLiveRoomVisitor extends PageVisitor {
    visitButton(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`action`) && node.text === `进入直播间`) {
            ctx.is = true;
        }
        if (node.id.endsWith(`action`) && node.text === `暂未直播，去个人主页`) {
            ctx.is = true;
        }
    }
}

export class IsMyFollowPageVisitor extends PageVisitor {
    visitRecyclerView(node: Ast, ctx: PageContext) {
        if (node.id.endsWith('live_my_follow_recycle_view')) {
            ctx.is = true;
        }
        node.children.map(c => c.visit(this, ctx))
    }
}

export function isMyFollowPage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new IsMyFollowPageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

export function isJoinLiveRoom() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new IsJoinLiveRoomVisitor().visitNode(root, ctx)
    return !!ctx.is;
}



class IsSettingCachePageVisitor extends PageVisitor {
    visitTextView(node: Ast, ctx: PageContext) {
        if (node.text === `存储空间` && node.sid === `title_tv` && node.parent.sid === `title_root`) {
            ctx.is = true;
        }
    }
}
export function isSettingCachePage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new IsSettingCachePageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

class IsSettingPageVisitor extends PageVisitor {
    visitTextView(node: Ast, ctx: PageContext) {
        if (node.text === `设置` && node.sid === `title_tv` && node.parent.sid === `title_root`) {
            ctx.is = true;
            return;
        }
        if (node.sid === `item_title` && node.text === `绑定手机号`) {
            ctx.is = true;
            return;
        }
        if (node.sid === `item_title` && node.text === `账号与安全`) {
            ctx.is = true;
            return;
        }
    }
}
export function isSettingPage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new IsSettingPageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}


export class TabLayoutPageVisitor extends PageVisitor {
    visitHorizontalScrollView(node: Ast, ctx: PageContext) {
        if (node.sid === `tab_layout`) {
            ctx.is = true;
        }
    }
}

export function isTabLayoutPage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new TabLayoutPageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

export function isLiveRoomPage() {
    const root = createRootAst()
    const ctx: PageContext = { is: false }
    new LiveRoomPageVisitor().visitNode(root, ctx)
    return !!ctx.is;
}

/**
 * 检查直播间红包是否已经打开
 */
export class IsRedOpenVisitor extends CoreVisitor {
    visitViewPager(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`live_red_packet_container_view_pager`) && node.visibleToUser) {
            ctx.is = true;
            return;
        }
        return node.children.map(it => it.visit(this, ctx))
    }
    visitTextView(node: Ast, ctx: PageContext) {
        if (node.text === `很遗憾，本次未中奖`) {
            ctx.is = true
            return;
        }
        if (node.text === `手慢了，补偿一张充值券`) {
            ctx.is = true
            return;
        }
        if (node.text === `很遗憾，本次任务失败`) {
            ctx.is = true
            return;
        }
        // todo
        if (node.text === `很遗憾，本次任务失败`) {
            ctx.is = true
            return;
        }
        if (node.text === `直播已结束`) {
            ctx.is = true
            return;
        }
    }
}
export function isRedOpen(): boolean {
    const root = createRootAst()
    let ctx: PageContext = {
        is: false
    }
    root.visit(new IsRedOpenVisitor(), ctx)
    return ctx.is;
}
/**
 * 检查直播间是否有等待开奖的红包
 */
export class HasRedWaitOpenVisitor extends CoreVisitor {
    visitTextView(node: Ast, ctx: PageContext) {
        if (node.id.endsWith(`live_condition_leee_button_text`)) {
            ctx.is = true;
        }
    }
}
export function hasRedWaitOpen() {
    const root = createRootAst()
    let ctx: PageContext = {
        is: false
    }
    root.visit(new HasRedWaitOpenVisitor(), ctx)
    return ctx.is;
}


/**
 * 

<android.widget.FrameLayout visible="true">
    <androidx.slidingpanelayout.widget.SlidingPaneLayout visible="true">
        <android.widget.FrameLayout visible="true">
        </android.widget.FrameLayout>
        <android.widget.FrameLayout visible="true">
            <androidx.viewpager.widget.ViewPager visible="true">
                <android.widget.FrameLayout id="com.kuaishou.nebula:id/left_btn" clickable="true" visible="true">
                </android.widget.FrameLayout>
                <android.widget.HorizontalScrollView id="com.kuaishou.nebula:id/kcube_tab_strip" visible="true">
                    <android.widget.FrameLayout clickable="true" visible="true">
                        <android.view.View id="com.kuaishou.nebula:id/textView" desc="直播" visible="true">
                        </android.view.View>
                    </android.widget.FrameLayout>
                    <android.widget.FrameLayout clickable="true" visible="true">
                        <android.view.View id="com.kuaishou.nebula:id/follow_tab_text" desc="同城" visible="true">
                        </android.view.View>
                    </android.widget.FrameLayout>
                    <android.widget.FrameLayout clickable="true" visible="true">
                    </android.widget.FrameLayout>
                    <android.widget.FrameLayout clickable="true" visible="true">
                        <android.view.View id="com.kuaishou.nebula:id/follow_tab_text" desc="关注" visible="true">
                        </android.view.View>
                    </android.widget.FrameLayout>
                    <android.widget.FrameLayout clickable="true" visible="true">
                        <android.view.View id="com.kuaishou.nebula:id/textView" desc="发现" visible="true">
                        </android.view.View>
                    </android.widget.FrameLayout>
                </android.widget.HorizontalScrollView>
                <android.widget.ImageView id="com.kuaishou.nebula:id/thanos_home_top_search" desc="查找" clickable="true" visible="true">
                </android.widget.ImageView>
                <androidx.viewpager.widget.ViewPager visible="true">
                	
                    <androidx.viewpager.widget.ViewPager >
                    </androidx.viewpager.widget.ViewPager>
                    <androidx.recyclerview.widget.RecyclerView id="com.kuaishou.nebula:id/recycler_view">
                        <android.widget.RelativeLayout id="com.kuaishou.nebula:id/container">
                            <android.widget.TextView id="com.kuaishou.nebula:id/subject" clickable="true">
                            </android.widget.TextView>
                        </android.widget.RelativeLayout>
                        <android.widget.RelativeLayout id="com.kuaishou.nebula:id/container">
                            <android.widget.TextView id="com.kuaishou.nebula:id/subject" clickable="true">
                            </android.widget.TextView>
                        </android.widget.RelativeLayout>
                        <android.widget.RelativeLayout id="com.kuaishou.nebula:id/container">
                            <android.widget.TextView id="com.kuaishou.nebula:id/subject" clickable="true">
                            </android.widget.TextView>
                        </android.widget.RelativeLayout>
                        <android.widget.RelativeLayout id="com.kuaishou.nebula:id/container">
                            <android.widget.TextView id="com.kuaishou.nebula:id/subject" clickable="true">
                            </android.widget.TextView>
                        </android.widget.RelativeLayout>
                        <android.widget.RelativeLayout id="com.kuaishou.nebula:id/container">
                            <android.widget.TextView id="com.kuaishou.nebula:id/subject" clickable="true">
                            </android.widget.TextView>
                        </android.widget.RelativeLayout>
                        <android.widget.RelativeLayout id="com.kuaishou.nebula:id/container">
                            <android.widget.TextView id="com.kuaishou.nebula:id/subject" clickable="true">
                            </android.widget.TextView>
                        </android.widget.RelativeLayout>
                    </androidx.recyclerview.widget.RecyclerView>
                    <android.widget.RelativeLayout id="com.kuaishou.nebula:id/home_local_sub_sizer_view" clickable="true">
                        <android.widget.LinearLayout id="com.kuaishou.nebula.nearby_header:id/city_layout" clickable="true">
                            <android.widget.TextView id="com.kuaishou.nebula.nearby_header:id/location_city" text="定位中...">
                            </android.widget.TextView>
                        </android.widget.LinearLayout>
                        <android.widget.TextView id="com.kuaishou.nebula.nearby_header:id/tv_function_message" text="交个朋友">
                        </android.widget.TextView>
                        <android.widget.TextView id="com.kuaishou.nebula.nearby_header:id/notificaiton_text">
                        </android.widget.TextView>
                    </android.widget.RelativeLayout>
                    <android.view.View id="com.kuaishou.nebula:id/slide_top_cover" clickable="true">
                    </android.view.View>
                    <androidx.viewpager.widget.ViewPager id="com.kuaishou.nebula:id/nasa_groot_view_pager" visible="true">
                        <android.widget.ImageView id="com.kuaishou.nebula:id/mask" visible="true">
                        </android.widget.ImageView>
                        <android.widget.TextView id="com.kuaishou.nebula:id/user_name_text_view" text="@黑k小ai" clickable="true" visible="true">
                        </android.widget.TextView>
                        <android.widget.ScrollView id="com.kuaishou.nebula:id/caption_scroll_container" visible="true">
                            <android.widget.TextView id="com.kuaishou.nebula:id/element_caption_label" text="为什么网吧两千的电脑比家里几万的… 展开" clickable="true" visible="true">
                            </android.widget.TextView>
                        </android.widget.ScrollView>
                        <android.widget.LinearLayout id="com.kuaishou.nebula:id/group_right_action_bar_root_layout" clickable="true" visible="true">
                            <android.view.ViewGroup id="com.kuaishou.nebula:id/slide_play_right_follow" clickable="true" visible="true">
                                <android.view.View id="com.kuaishou.nebula:id/slide_play_avatar_click_area" clickable="true" visible="true">
                                </android.view.View>
                                <android.widget.ImageView id="com.kuaishou.nebula:id/slide_play_right_follow_button" clickable="true" visible="true">
                                </android.widget.ImageView>
                            </android.view.ViewGroup>
                            <android.widget.FrameLayout id="com.kuaishou.nebula:id/like_button" clickable="true" visible="true">
                                <android.view.View id="com.kuaishou.nebula:id/click_area" clickable="true" visible="true">
                                </android.view.View>
                                <android.widget.TextView id="com.kuaishou.nebula:id/like_count_view" text="7751" visible="true">
                                </android.widget.TextView>
                            </android.widget.FrameLayout>
                            <android.widget.FrameLayout id="com.kuaishou.nebula:id/comment_button" clickable="true" visible="true">
                                <android.view.View id="com.kuaishou.nebula:id/click_area" clickable="true" visible="true">
                                </android.view.View>
                                <android.widget.TextView id="com.kuaishou.nebula:id/comment_count_view" text="138" visible="true">
                                </android.widget.TextView>
                            </android.widget.FrameLayout>
                            <android.view.ViewGroup id="com.kuaishou.nebula:id/collect_button" clickable="true" visible="true">
                                <android.view.View id="com.kuaishou.nebula:id/click_area" clickable="true" visible="true">
                                </android.view.View>
                                <android.widget.TextView id="com.kuaishou.nebula:id/collect_text" text="5576" visible="true">
                                </android.widget.TextView>
                            </android.view.ViewGroup>
                            <android.widget.FrameLayout id="com.kuaishou.nebula:id/forward_button" clickable="true" visible="true">
                                <android.view.View id="com.kuaishou.nebula:id/click_area" clickable="true" visible="true">
                                </android.view.View>
                                <android.widget.TextView id="com.kuaishou.nebula:id/forward_count" text="分享" visible="true">
                                </android.widget.TextView>
                            </android.widget.FrameLayout>
                            <android.widget.FrameLayout id="com.kuaishou.nebula:id/music_wheel_container" clickable="true" visible="true">
                            </android.widget.FrameLayout>
                        </android.widget.LinearLayout>
                        <android.widget.FrameLayout id="com.kuaishou.nebula:id/gesture_mask_layer" clickable="true">
                        </android.widget.FrameLayout>
                        <android.widget.TextView id="com.kuaishou.nebula:id/live_cover_icon_text" text="直播中">
                        </android.widget.TextView>
                        <android.widget.TextView id="com.kuaishou.nebula:id/user_name_text_view_new" text="@嫚婷x" clickable="true">
                        </android.widget.TextView>
                        <android.widget.TextView id="com.kuaishou.nebula:id/live_preview_bottom_caption" text="你的哇塞女孩" clickable="true">
                        </android.widget.TextView>
                    </androidx.viewpager.widget.ViewPager>
                    <android.widget.TextView id="com.kuaishou.nebula:id/picture_tag_text">
                    </android.widget.TextView>
                    <android.widget.LinearLayout id="com.kuaishou.nebula:id/profile_feed_avatar_wrapper" clickable="true">
                        <android.widget.TextView id="com.kuaishou.nebula:id/profile_feed_title" text="她的作品">
                        </android.widget.TextView>
                        <android.widget.TextView id="com.kuaishou.nebula:id/global_mine_photos_count">
                        </android.widget.TextView>
                    </android.widget.LinearLayout>
                    <androidx.recyclerview.widget.RecyclerView id="com.kuaishou.nebula:id/profile_photos_recycler_view">
                    </androidx.recyclerview.widget.RecyclerView>
                </androidx.viewpager.widget.ViewPager>
            </androidx.viewpager.widget.ViewPager>
            <android.widget.HorizontalScrollView id="com.kuaishou.nebula:id/tab_layout" visible="true">
                <androidx.appcompat.app.ActionBar$c desc="首页" clickable="true" visible="true">
                    <android.widget.CheckedTextView id="android:id/text1" text="首页" visible="true">
                    </android.widget.CheckedTextView>
                </androidx.appcompat.app.ActionBar$c>
                <androidx.appcompat.app.ActionBar$c desc="朋友" clickable="true" visible="true">
                    <android.widget.CheckedTextView id="android:id/text1" text="朋友">
                    </android.widget.CheckedTextView>
                </androidx.appcompat.app.ActionBar$c>
                <androidx.appcompat.app.ActionBar$c desc="消息" clickable="true" visible="true">
                    <android.widget.CheckedTextView id="android:id/text1" text="消息" visible="true">
                    </android.widget.CheckedTextView>
                </androidx.appcompat.app.ActionBar$c>
                <androidx.appcompat.app.ActionBar$c desc="我" clickable="true" visible="true">
                    <android.widget.CheckedTextView id="android:id/text1" text="我" visible="true">
                    </android.widget.CheckedTextView>
                </androidx.appcompat.app.ActionBar$c>
            </android.widget.HorizontalScrollView>
            <android.view.ViewGroup id="com.kuaishou.nebula:id/shoot_container" clickable="true" visible="true">
            </android.view.ViewGroup>
        </android.widget.FrameLayout>
    </androidx.slidingpanelayout.widget.SlidingPaneLayout>
    <android.widget.ImageView id="com.kuaishou.nebula:id/normal_pendant" clickable="true" visible="true">
    </android.widget.ImageView>
    <android.widget.ImageView id="com.kuaishou.nebula:id/close_pendant" clickable="true" visible="true">
    </android.widget.ImageView>
    <android.widget.FrameLayout clickable="true" visible="true">
    </android.widget.FrameLayout>
</android.widget.FrameLayout>
 */


export class DialogVisitor extends PageVisitor {
    visitFrameLayout(node: Ast, ctx: PageContext) {
        if (!node.parent) {
            const { left, right, top, bottom } = node.bounds;
            ctx.is = top > 0;
            return;
        }
    }
}
export class HomeJingXuanPageVisitor extends PageVisitor {
    visitNode(node: Ast, ctx: PageContext) {
        if (node.sid === `nasa_groot_view_pager` && node.visibleToUser) {
            ctx.is = true;
            return;
        }
        super.visitNode(node, ctx)
    }
    visitViewPager(node: Ast, ctx: PageContext) {
        if (node.sid === `nasa_groot_view_pager` && node.visibleToUser) {
            ctx.is = true;
            return;
        }
        return node.children.map(child => child.visit(this, ctx))
    }
    visitTextView(node: Ast, ctx: PageContext) {
        if (node.sid === `live_simple_play_swipe_text` && node.visibleToUser) {
            ctx.is = true;
            return;
        }
    }
}
export function isHomeJingXuanPage(pkg: string = PACKAGES.KUAI_SHOU) {
    const root = createRootAst()
    let ctx: PageContext = {
        is: false
    }
    root.visit(new HomeJingXuanPageVisitor(), ctx)
    return ctx.is;
}

export function isDialog() {
    const root = createRootAst()
    let ctx: PageContext = {
        is: false
    }
    root.visit(new DialogVisitor(), ctx)
    return ctx.is;
}
